package com.har.modules.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.har.common.exception.RRException;
import com.har.common.utils.PageUtils;
import com.har.common.utils.Query;
import com.har.common.utils.StringUtil;
import com.har.config.HtConfig;
import com.har.modules.web.dao.DefinedSafeDao;
import com.har.modules.web.dao.InsuredDao;
import com.har.modules.web.dao.LegalDao;
import com.har.modules.web.dao.LoanHtDao;
import com.har.modules.web.entity.*;
import com.har.modules.web.entity.extend.*;
import com.har.modules.web.service.DefinedSafeService;
import com.har.modules.web.service.InsuredService;
import com.har.modules.web.service.LegalService;
import com.har.modules.web.service.LoanHtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("loanHtService")
public class LoanHtServiceImpl extends ServiceImpl<LoanHtDao, LoanHtEntity> implements LoanHtService {


    @Autowired
    private HtConfig htConfig;
    @Autowired
    private InsuredDao insuredDao;
    @Autowired
    private LegalDao legalDao;
    @Autowired
    private DefinedSafeDao definedSafeDao;
    @Autowired
    private InsuredService insuredService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<LoanHtEntity> page = this.selectPage(
                new Query<LoanHtEntity>(params).getPage(),
                new EntityWrapper<LoanHtEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PolicyModel policy(String orderNo) {
        if(StringUtil.isEmpty(orderNo)){
            throw new RRException("订单号为空");
        }
        log.info("投保订单号：{}",orderNo);

        PolicyModel policyModel=new PolicyModel();

        //根据订单号查询订单是否存在
        EntityWrapper<LoanHtEntity> ew=new EntityWrapper<>();

        LoanHtEntity loanHtEntity=this.selectOne(ew.eq("order_no",orderNo));
        if(null==loanHtEntity){
            throw new RRException("该订单不存在");
        }
        //渠道信息
        ChannelEntity channelEntity=new ChannelEntity();
        channelEntity.setBizCode("101");
        channelEntity.setChannelCode(htConfig.getChannelCode());
        channelEntity.setCreateTime(new Date());
        channelEntity.setOrderId(orderNo);
        policyModel.setChannelObject(channelEntity);
        //险种信息
        InsuranceEntity insuranceEntity=new InsuranceEntity();
        insuranceEntity.setAmount(loanHtEntity.getAmount());
        insuranceEntity.setAmtCur(loanHtEntity.getAmtCur());
        insuranceEntity.setCopy(loanHtEntity.getCopy());
        insuranceEntity.setDocsn(loanHtEntity.getDocsn());
        insuranceEntity.setDocType(loanHtEntity.getDocType());
        insuranceEntity.setEffectiveTime(loanHtEntity.getEffectiveTime());
        insuranceEntity.setInsuranceCode(loanHtEntity.getInsuranceCode());
        insuranceEntity.setPlan(loanHtEntity.getPlan());
        insuranceEntity.setPremium(loanHtEntity.getPremium());
        insuranceEntity.setPrmCur(loanHtEntity.getPrmCur());
        insuranceEntity.setInsuranceName(loanHtEntity.getInsuranceName());
        insuranceEntity.setRate(loanHtEntity.getRate());
        insuranceEntity.setTerminalTime(loanHtEntity.getTerminalTime());
        policyModel.setInsuranceObject(insuranceEntity);
        //投保人信息
        AppntEntity appntEntity=new AppntEntity();
        appntEntity.setAppAddr(loanHtEntity.getAppAddr());
        appntEntity.setAppBirthday(loanHtEntity.getAppBirthday());
        appntEntity.setAppContact(loanHtEntity.getAppContact());
        appntEntity.setAppEmail(loanHtEntity.getAppEmail());
        appntEntity.setAppGender(loanHtEntity.getAppGender());
        appntEntity.setAppidType(loanHtEntity.getAppidType());
        appntEntity.setAppName(loanHtEntity.getAppName());
        appntEntity.setAppNumber(loanHtEntity.getAppNumber());
        appntEntity.setIsTaxInvoice(loanHtEntity.getIsTaxInvoice());
        appntEntity.setAppType(loanHtEntity.getAppType());
        //当isTaxInvoice为1时必填
        if(1==loanHtEntity.getIsTaxInvoice()){
            appntEntity.setAppTelNumber(loanHtEntity.getAppTelNumber());
            appntEntity.setTaxCertifi(loanHtEntity.getTaxCertifi());
            appntEntity.setDepositBank(loanHtEntity.getDepositBank());
            appntEntity.setBankAccount(loanHtEntity.getBankAccount());
        }
        policyModel.setAppntObject(appntEntity);

        //被保人列表
        List<InsuredModel> insuredObject=insuredDao.selectListByLoadId(loanHtEntity.getId());

        for (InsuredModel insuredModel:insuredObject) {
            log.info("被保人id:",insuredModel.getId());
            //受益人详细信息列表
            List<LegalModel> benefitDTOs = legalDao.selectListById(insuredModel.getId());
            insuredModel.setId(null);
            insuredModel.setBenefitDTOs(benefitDTOs);
        }
        policyModel.setInsuredObject(insuredObject);

        //自定义责任信息
        List<RdrModel> rdrModels=definedSafeDao.selectListById(loanHtEntity.getId());
        DefinedSafeModel definedSafeModel=new DefinedSafeModel();
        definedSafeModel.setIsDefinedSafe(loanHtEntity.getIsDefinedSafe());
        definedSafeModel.setRdrDTOs(rdrModels);
        policyModel.setDefinedSafeObj(definedSafeModel);

        //支付信息
        PayEntity payEntity=new PayEntity();
        //默认为1
        payEntity.setIsSinglepay(loanHtEntity.getIsSinglepay());
        //当isSinglepay为1时 下面必填
        if(1==loanHtEntity.getIsSinglepay()){
            payEntity.setPayBankNo(loanHtEntity.getPayBankNo());
            payEntity.setPaydate(loanHtEntity.getPaydate());
            payEntity.setPayMode(loanHtEntity.getPayMode());
        }
        policyModel.setPayObject(payEntity);
        //约定信息
        AgreementEntity agreementEntity=new AgreementEntity();
        agreementEntity.setPolicyDeductible(loanHtEntity.getPolicyDeductible());
        agreementEntity.setPolicySpec(loanHtEntity.getPolicySpec());
        policyModel.setAgreementObject(agreementEntity);
        //产品差异字段
        ProductDiffEntity productDiffEntity=new ProductDiffEntity();
        productDiffEntity.setRemark(loanHtEntity.getRemark());
        policyModel.setProductDiffObject(productDiffEntity);

        return policyModel;
    }

    @Override
    public void addOrder(LoanHtModel loanHtModel) {

        //先插入保险主表
        LoanHtEntity loanHt=new LoanHtEntity();
        loanHt.setInsuranceCode(null);
        loanHt.setInsuranceName(loanHtModel.getInsuranceName());
        loanHt.setAmount(loanHtModel.getAmount());
        loanHt.setAppEmail(loanHtModel.getAppEmail());
        loanHt.setAppName(loanHtModel.getAppName());
        loanHt.setAppNumber(loanHtModel.getAppNumber());
        loanHt.setAppGender(loanHtModel.getAppGender());
        loanHt.setAmtCur(loanHtModel.getAmtCur());
        loanHt.setAppBirthday(loanHtModel.getAppBirthday());
        loanHt.setAppidType(01);
        loanHt.setAppContact(loanHtModel.getAppContact());
        loanHt.setAppTelNumber(loanHtModel.getAppTelNumber());
        loanHt.setAppAddr(loanHtModel.getAppAddr());
        loanHt.setDepositBank(loanHtModel.getDepositBank());
        loanHt.setAppType(loanHtModel.getAppType());
        loanHt.setCopy(loanHtModel.getCopy());
        loanHt.setBankAccount(loanHtModel.getBankAccount());
        loanHt.setCreateTime(new Date());
        loanHt.setDocsn(loanHtModel.getDocsn());
        loanHt.setEffectiveTime(loanHtModel.getEffectiveTime());
        loanHt.setPremium(loanHtModel.getPremium());
        loanHt.setTerminalTime(loanHtModel.getTerminalTime());
        loanHt.setRemark(loanHtModel.getRemark());
        loanHt.setTaxCertifi(loanHtModel.getTaxCertifi());
        loanHt.setPlan("A");
        loanHt.setAppAddr(loanHtModel.getAppAddr());
        loanHt.setDocType(loanHtModel.getDocType());
        loanHt.setPolicySpec(loanHtModel.getPolicySpec());
        loanHt.setPrmCur("01");
        loanHt.setDepositBank(loanHtModel.getDepositBank());
        loanHt.setAppType(1);
        loanHt.setAppGender(9);
        //是否打印增值税发票  默认 0：否1：是
        loanHt.setIsTaxInvoice(0);
        //是否逐单支付  0：否    默认1：是
        loanHt.setIsSinglepay(1);
        this.insert(loanHt);

        //添加被保人
        InsuredEntity insuredEntity=new InsuredEntity();
        insuredEntity.setLoanId(loanHt.getId());
        //被保人类型(默认为1 ，1：个人 2：团体)
        insuredEntity.setInsuredType(1);
        insuredEntity.setInsuredNumber(loanHtModel.getAppNumber());
        //被保人性别(1：男；2：女；9：未说明)
        insuredEntity.setInsuredGender(9);
        insuredEntity.setInsuredName(loanHtModel.getAppName());
        insuredEntity.setInsuredEmail(loanHtModel.getAppEmail());
        insuredEntity.setInsuredTelNumber(loanHtModel.getAppTelNumber());
        //是否是法定受益人(	0：否 1：是（默认）如是则无需传递受益人详细信息)
        insuredEntity.setIslegal(1);
        insuredEntity.setRelationship(loanHtModel.getRelationship());
        insuredEntity.setInsuredAddress(loanHtModel.getAppAddr());
        insuredEntity.setInsuredType(loanHtModel.getAppidType());
        //该保险为身份证 默认为01
        insuredEntity.setInsuredIdType("01");
        insuredService.insert(insuredEntity);

    }

}
